package com.class04.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2023/12/27 14:43
 */
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle：在请求处理之前执行，返回值决定是否继续处理请求。" +
                "如果返回true，则继续处理请求；如果返回false，则中断请求处理。");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle：在请求处理之后，视图渲染之前执行。" +
                "通常用于对请求处理过程中的数据进行后处理，例如修改视图数据、记录日志等。" +
                "这个方法只接收当前处理的请求和响应对象作为参数。");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion：在整个请求处理完成后执行，也就是在视图渲染之后。" +
                "通常用于进行资源清理操作，例如关闭数据库连接、释放资源等。" +
                "这个方法同样只接收当前处理的请求和响应对象作为参数");
    }
}
